package project.Dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import project.Pojo.Doctor;
import project.Pojo.Patient;
import project.Service.UserService;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatis {
    @Qualifier("doctor")
    UserService userService;
    //1、获取mybatis引擎，设置全局方便函数调用
    private static String resource = "mybatis-config.xml";
    private static InputStream inputStream;

    static {
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    //2、获取用于执行sql语句的sqlSession
    private static SqlSession sqlSession = sqlSessionFactory.openSession();

    public MyBatis() throws IOException {
    }

    //3、查询表信息以List储存
    public List<Doctor> getDoctorList() {                                        //医生表
        return sqlSession.selectList("doctor.selectAll");
    }

    public List<Patient> getPatientList() {                                          //患者信息表
        return sqlSession.selectList("patient.selectAll");
    }

    public static void main(String[] args) throws IOException {
        MyBatis test = new MyBatis();
        System.out.println(test.getDoctorList());
        System.out.println(test.getPatientList());
        System.out.println(System.currentTimeMillis() / 1000000);
    }
}
